package org.jeecg.modules.demo.task.taskImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.RestUtil;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.taskContext.entity.TTaskContext;
import org.jeecg.modules.demo.taskContext.service.ITTaskContextService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: FindTaskContext
 * @Description:
 * @Author: 墨渍
 * @Date: 2025/4/10 10:30
 **/
@RestController
@RequestMapping("/task")
public class FindTaskContext extends JeecgController<TTaskContext, ITTaskContextService> {

    @Resource
    private ITTaskContextService tTaskContextService;


    @IgnoreAuth
    public void findComment(String reqNoteId, String token,String realUserId){
        String url = "http://27.30.78.111:5643/xhsapi/comment";
        HttpHeaders headers = new HttpHeaders();
        JSONObject param = new JSONObject();
//        param.put("noteId","677d1909000000002002a892");
        param.put("noteId",reqNoteId);
        headers.add("Authorization","c402bea00356c04b7100f2c82e6bc71c");
        ResponseEntity<JSONObject> jsonObjectResponseEntity = RestUtil.get(url, headers, param);
        JSONObject body = jsonObjectResponseEntity.getBody();
        System.out.println(body);
        List<JSONObject> list = new ArrayList<>();
        if (null != body){
            Long balance = body.getLong("balance");
            Integer code = body.getInteger("code");
            if (!code.equals(10200)){
                return;
            }
            JSONObject jsonObject = body.getJSONObject("body");
            if (null != jsonObject){
                if (!"0".equals(jsonObject.getString("code")) || !jsonObject.getBoolean("success")){
                    return;
                }
                JSONObject data = jsonObject.getJSONObject("data");
                if (null == data){
                    return;
                }
                JSONArray comments = data.getJSONArray("comments");
                int size = comments.size();
                if (comments.isEmpty()){
                    return;
                }
                List<TTaskContext> taskContextList = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    TTaskContext  taskContext = new TTaskContext();
                    StringBuilder picUrls = new StringBuilder();
                    JSONObject subComments = new JSONObject();
                    JSONObject comment = comments.getJSONObject(i);
                    String noteId = comment.getString("note_id");
                    //笔记id
                    taskContext.setNoteId(noteId);
                    taskContext.setLikeCount(comment.getInteger("like_count"));//评论点赞数量
                    taskContext.setSubCommentCount(comment.getInteger("sub_comment_count"));//子评论数量
                    taskContext.setContent(comment.getString("content"));//评论内容
                    taskContext.setComId(comment.getString("id"));//评论id
                    taskContext.setIpLocation(comment.getString("ip_location"));//定位
                    JSONObject user = comment.getJSONObject("user");

                    String userId = user.getString("userid");
                    String nickname = user.getString("nickname");
                    String red_id = user.getString("red_id");
                    taskContext.setComUserId(userId);//评论用户id
                    taskContext.setNickname(nickname);//用户昵称
                    taskContext.setRedId(red_id);//用户rid
                    taskContext.setCreateTime(new Date());//首次采集时间
                    taskContext.setTime(new Date(comment.getLong("time")*1000));//发布时间
                    taskContext.setUserId(realUserId);
//                    taskContext.setUserId(userId);//用户id
                    subComments.put("create_time",red_id);
//                    subComments.put("update_time",red_id);//更新时间
                    JSONArray pictures = comment.getJSONArray("pictures");
                    if (pictures!=null&&!pictures.isEmpty()){
                        int picSize = pictures.size();
                        for (int j = 0; j < picSize; j++) {
                            JSONObject picture = pictures.getJSONObject(j);
                            picUrls.append(",").append(picture.getString("url"));
                        }
                    }
                    taskContext.setPicturesUrl(picUrls.toString());//评论图片列表，逗号分隔
                    taskContext.setIsNotice(0);
                    try {
                        tTaskContextService.save(taskContext);
                    }catch (DuplicateKeyException ex){
                        System.out.println(ex);
                    }catch (Exception e){
                        System.out.println(e);
                    }

                }
            }
        }
        System.out.println(list);
    }

    public static void main(String[] args) {
        String url = "http://27.30.78.111:5643/xhsapi/comment";
        HttpHeaders headers = new HttpHeaders();
        JSONObject param = new JSONObject();
        param.put("noteId","677d1909000000002002a892");
        headers.add("Authorization","c402bea00356c04b7100f2c82e6bc71c");
        ResponseEntity<JSONObject> jsonObjectResponseEntity = RestUtil.get(url, headers, param);
        JSONObject body = jsonObjectResponseEntity.getBody();
        System.out.println(body);
        List<JSONObject> list = new ArrayList<>();
        if (null != body){
            Long balance = body.getLong("balance");
            Integer code = body.getInteger("code");
            if (!code.equals(10200)){
                return;
            }
            JSONObject jsonObject = body.getJSONObject("body");
            if (null != jsonObject){
                if (!"0".equals(jsonObject.getString("code")) || !jsonObject.getBoolean("success")){
                    return;
                }
                JSONObject data = jsonObject.getJSONObject("data");
                if (null == data){
                    return;
                }
                JSONArray comments = data.getJSONArray("comments");
                System.out.println(comments);
                int size = comments.size();
                if (comments.isEmpty()){
                    return;
                }
                for (int i = 0; i < size; i++) {
                    StringBuilder picUrls = new StringBuilder();
                    JSONObject subComments = new JSONObject();
                    JSONObject comment = comments.getJSONObject(i);
                    String noteId = comment.getString("note_id");
                    subComments.put("node_id",noteId);//笔记id
                    subComments.put("like_count",comment.getString("like_count"));//评论点赞数量
                    subComments.put("sub_comment_count",comment.getString("sub_comment_count"));//子评论数量
                    subComments.put("content",comment.getString("content"));//评论内容
                    subComments.put("id",comment.getString("id"));//评论id
                    subComments.put("ip_location",comment.getString("ip_location"));//定位
                    JSONObject user = comment.getJSONObject("user");
                    String userId = user.getString("userid");
                    String nickname = user.getString("nickname");
                    String red_id = user.getString("red_id");
                    subComments.put("userid",userId);//用户id
                    subComments.put("nickname",nickname);//用户昵称
                    subComments.put("red_id",red_id);//用户rid
                    subComments.put("create_time",red_id);//首次采集时间
                    subComments.put("update_time",red_id);//更新时间
                    subComments.put("time",comment.getLong("time"));//发布时间
                    JSONArray pictures = comment.getJSONArray("pictures");
                    if (pictures!=null&&!pictures.isEmpty()){
                        int picSize = pictures.size();
                        for (int j = 0; j < picSize; j++) {
                            JSONObject picture = pictures.getJSONObject(j);
                            picUrls.append(",").append(picture.getString("url"));
                        }
                    }
                    subComments.put("pictures_url", picUrls);//评论图片列表，逗号分隔
                    subComments.put("is_notice",0);
                    list.add(subComments);
                }
            }
        }
        System.out.println(list);

    }
}
